﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SqlToLua
{
    class SpecialFunction
    {
        public static void AppendFunction(StringBuilder sb, string table)
        {
            switch (table)
            {
                #region ConfigActive
                case "ConfigActive":
                    sb.Append("\r\n\r\nfunction getInTimeActivity()");
                    sb.Append("\r\n    ConfigActive.sort()");
                    sb.Append("\r\n");
                    sb.Append("\r\n    local tempCanGetTable = {}");
                    sb.Append("\r\n    local tempOtherTable = {}");
                    sb.Append("\r\n    local tempTable = {}");
                    sb.Append("\r\n");
                    sb.Append("\r\n    for i=1, #configTable do");
                    sb.Append("\r\n        local obj = configTable[i]");
                    sb.Append("\r\n        local isInTime = false");
                    sb.Append("\r\n        if obj.show == 0 then");
                    sb.Append("\r\n            if obj.ever == 1 then");
                    sb.Append("\r\n                if (obj.start_time and obj.end_time) then");
                    sb.Append("\r\n                    isInTime = TimeUtils.isTimeIn(obj.start_time, obj.end_time)");
                    sb.Append("\r\n                else");
                    sb.Append("\r\n                    isInTime = true");
                    sb.Append("\r\n                end");
                    sb.Append("\r\n            else");
                    sb.Append("\r\n                isInTime = DataManager.isActiveInTime(obj.type)");
                    sb.Append("\r\n            end");
                    sb.Append("\r\n--            if obj.id == 6 or obj.id == 7 then");
                    sb.Append("\r\n--                local targetPlatform = cc.Application:getInstance():getTargetPlatform()");
                    sb.Append("\r\n--                if cc.PLATFORM_OS_IPHONE == targetPlatform or cc.PLATFORM_OS_IPAD == targetPlatform  then");
                    sb.Append("\r\n--                    isInTime = false");
                    sb.Append("\r\n--                end");
                    sb.Append("\r\n--            end");
                    sb.Append("\r\n        end");
                    sb.Append("\r\n        if obj.type == 13 or obj.type == 14 then");
                    sb.Append("\r\n        --isInTime = true");
                    sb.Append("\r\n        end");
                    sb.Append("\r\n        if isInTime then");
                    sb.Append("\r\n            local complete = false");
                    sb.Append("\r\n            if obj.id == 2 then");
                    sb.Append("\r\n                complete = ActivityRewardUtils.checkLoginDressComplete()");
                    sb.Append("\r\n            elseif obj.id == 1 then");
                    sb.Append("\r\n                complete = ActivityRewardUtils.checkLvlRewardComplete()");
                    sb.Append("\r\n            elseif obj.id == 5 then");
                    sb.Append("\r\n                complete = ActivityRewardUtils.checkFirstPayComplete()");
                    sb.Append("\r\n            end");
                    sb.Append("\r\n            if not complete then");
                    sb.Append("\r\n                local canGet = ActivityRewardUtils.checkCanGetByType(obj.type)");
                    sb.Append("\r\n                if canGet then");
                    sb.Append("\r\n                    table.insert(tempCanGetTable, obj)");
                    sb.Append("\r\n                else");
                    sb.Append("\r\n                    table.insert(tempOtherTable, obj)");
                    sb.Append("\r\n                end");
                    sb.Append("\r\n            end");
                    sb.Append("\r\n        end");
                    sb.Append("\r\n    end");
                    sb.Append("\r\n    for i=1, #tempCanGetTable do");
                    sb.Append("\r\n        local tempObj = tempCanGetTable[i]");
                    sb.Append("\r\n        table.insert(tempTable, tempObj)");
                    sb.Append("\r\n    end");
                    sb.Append("\r\n    for i=1, #tempOtherTable do");
                    sb.Append("\r\n        local tempObj = tempOtherTable[i]");
                    sb.Append("\r\n        table.insert(tempTable, tempObj)");
                    sb.Append("\r\n    end");
                    sb.Append("\r\n    return tempTable");
                    sb.Append("\r\nend");

                    sb.Append("\r\n\r\nfunction sort()");
                    sb.Append("\r\n    local function sortFunc(a, b)");
                    sb.Append("\r\n        return tonumber(a.view_order) < tonumber(b.view_order)");
                    sb.Append("\r\n    end");
                    sb.Append("\r\n    table.sort(configTable, sortFunc)");
                    sb.Append("\r\nend");
                    break;
                #endregion

                case "ConfigSq":
                    sb.Append("\r\n\r\nfunction getDataByOldEquip(itemId)");
                    sb.Append("\r\n    for key, var in pairs(configTable) do");
                    sb.Append("\r\n        if var[\"zb1\"] == itemId then");
                    sb.Append("\r\n            return var");
                    sb.Append("\r\n        end");
                    sb.Append("\r\n    end");
                    sb.Append("\r\n    return nil");
                    sb.Append("\r\nend");
                    break;

                case "ConfigSqStep":
                    sb.Append("\r\n\r\nfunction getDataByOldEquip(itemId)");
                    sb.Append("\r\n    for key, var in pairs(configTable) do");
                    sb.Append("\r\n        if var[\"sq1\"] == itemId then");
                    sb.Append("\r\n            return var");
                    sb.Append("\r\n        end");
                    sb.Append("\r\n    end");
                    sb.Append("\r\n    return nil");
                    sb.Append("\r\nend");
                    break;
                default:
                    break;
            }
        }
    }
}
